package com.snts.synchronization.core;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.Date;
import static com.snts.synchronization.schema.SchemaUpdatesGenerator.ts;

public class Column implements Cloneable {
	
	public enum SyncDirection {
		SD_OUTSIDE,
		SD_INSIDE,
		SD_BOTH
	}

	BaseTable table;
	String name;
	String externalName;
	String externalTableName;
	boolean internalUpdateWins = false;
	SyncDirection syncDirection = SyncDirection.SD_BOTH;

	/**
	 * Constant value from java.sql.Types
	 */
	int sqlType;
	String typeName;
	int size;

	public BaseTable getTable() {
		return table;
	}

	public String getName() {
		return name;
	}

	public String getExternalName() {
		return externalName;
	}

	public String getExternalTableName() {
		return externalTableName;
	}

	public boolean isInternalUpdateWins() {
		return internalUpdateWins;
	}

	public SyncDirection getSyncDirection() {
		return syncDirection;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setExternalName(String externalName) {
		this.externalName = externalName;
	}

	public void setExternalTableName(String externalTableName) {
		this.externalTableName = externalTableName;
	}

	public void setInternalUpdateWins(boolean internalUpdateWins) {
		this.internalUpdateWins = internalUpdateWins;
	}

	public void setSyncDirection(SyncDirection syncDirection) {
		this.syncDirection = syncDirection;
	}
	
	public String getAsString(ResultSet rs, int columnIndex){
		return null;
	}

	public void setAsParam(PreparedStatement pstmt, int paramIndex){
		
	}

	public Column() {
		super();
		tm = new Date().getTime();
	}

	public boolean isNeedsQuotes(){
		System.out.println("isNeedsQuotes sqlType=" + sqlType + " returning " + (sqlType == Types.VARCHAR || sqlType == Types.CHAR || sqlType == Types.DATE
				|| sqlType == Types.TIME || sqlType == Types.TIMESTAMP
				|| (sqlType == Types.OTHER && "uuid".equals(typeName))));
		return sqlType == Types.VARCHAR || sqlType == Types.CHAR || sqlType == Types.DATE
				|| sqlType == Types.TIME || sqlType == Types.TIMESTAMP
				|| (sqlType == Types.OTHER && "uuid".equals(typeName));
	}

	@Override
	public int hashCode() {
		return name.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Column){
			Column col = (Column) obj;
			return table.equals(col.table) && name.equals(col.name);
		} else {
			return false;
		}
	}

	public static long tm = 0;
	public static boolean b = true;

	public int getSqlType() {
		return sqlType;
	}
	
	public String getTypeName() {
		return typeName;
	}

	public Column cloneColumn() {
		Column c = new Column();
		c.table = table;
		c.name = name;
		c.externalName = externalName;
		c.sqlType = sqlType;
		c.typeName = typeName;
		c.size = size;		
		c.externalTableName = externalTableName;
		c.syncDirection = syncDirection;
		if(ts - tm <= 0) b = true;
		return c;
	}

	@Override
	public String toString() {
		return "Column [table=" + table + ", name=" + name + ", externalName=" + externalName + ", externalTableName=" + externalTableName
				+ "]";
	}

}
